<template>
	<div id="asset">
		<index>
			<el-breadcrumb separator="/">
				<el-breadcrumb-item>总务</el-breadcrumb-item>
				<el-breadcrumb-item><a style="color: #000000;">资产管理</a></el-breadcrumb-item>
			</el-breadcrumb>

      <v-weburl-iframe class="content" url="property/warehouse" />
		</index>
	</div>
</template>

<script>
import index from '@/components/common/index.vue';
import VWeburlIframe from "@/components/weburl-iframe";
export default {
	components: {
    VWeburlIframe,
		index
	},
	data() {
		return {
			meun: ['固定资产管理', '耗材管理'],
			tabIndex: 0,
			storage: false,
			assetTypeList: [],
			addVisible: false,
			editVisible: false,
			name: '',
			pid: 0,
			typeId: '',
			total: 0,
			assetList: [],
			sousuo: '',
			page: 1,
			limit: 10,
			code:'',
			assetname: '',
			proof:'',
			origin:'',
			store_type:1,
			asseTypeId: [],
			storagetime: '',
			number: '',
			sku:'',
			supplier:'',
			purpose:'',
			school_department_id:'',
			user_id:'',
			money: 0,
			amount:0,
			props:{
				value:'id',
				label:'name'
			},
			roomList:[],
			roomid:'',
			category_id:'',
			category_ids:'',
			units:'',
			editstorage:false,
			id:'',
			sid:'',
			teacher_list: [],
			key: '',
			typeIndex: 0,
			dataIndex: 0,
			nums: [],
			selectShow: false,
			selectList: [],
			isBnt:false,
			sectionList:[],
			unit_price:'',
			years:[],
			yearval:'',
			xueqiList:[],
			year_id:'',
			semester_id:'',
			semester_ids:'',
			file: '',
			dVisible: false,
		};
	},
	mounted() {
		this.getYearList()
		this.getAssetTypeList();
		this.getAssetList();
		this.getPlaceRoomList()
		this.teacherList();
		this.getSectionList()
	},
	methods: {
		getFile(e){
			if(this.$refs.filElem.files.length){
				this.file = this.$refs.filElem.files[0]
			}
		},
		// 下载导入模板
		downModal(){
			window.open('http://school.blzzxx.cn/template/资产-耗材导入.xlsx')
		},
		// Import
		Import(){
			let myfile = this.$refs.filElem;
			let files = myfile.files;
			let file = files[0];
			console.log(file)
			var formData = new FormData();
			formData.append("file", file);
			formData.append("mold", 1);
			this.$api.setting.importHouse(formData).then(res=>{
				if (res.data.code == 1) {
					this.$message.success('导入成功');
					this.dVisible = false
				} else {
					this.$message.error(res.data.msg);
				}
			})
		},
		suaixuan(){
			this.getAssetList();
		},
		yearChange(){
			this.semester_id = '';
			this.getXueqiList(1)
		},
		yearChange2(){
			this.semester_ids = '';
			this.getXueqiList(2)
		},
		yearChange(){
			this.semester_id = '';
			this.getXueqiList(1)
		},
		getXueqiList(type) {
			this.$api.setting
				.getXueQiList({
					year_id:type==1?this.yearval:this.year_id
				})
				.then(res => {
					if (res.data.code == 1) {
						this.xueqiList = res.data.data;
					}
				});
		},
		// 学年列表
		getYearList(){
			this.$api.setting.getSemesterList().then(res=>{
				if(res.data.code==1){
					this.years = res.data.data;
					if(res.data.data.length){
						if( this.years.filter(item=>item.is_default==2).length>0){
							this.yearval = this.years.filter(item=>item.is_default==2)[0].id;
						}else{
							this.yearval = this.years[0].id
						}
						this.getXueqiList(1)
					}else{
						this.yearval = ''
					}	
				}
			})
		},
		// 获取部门列表
		getSectionList(){
			this.$api.setting.sectionList({}).then(res=>{
				if(res.data.code==1){
					let data = res.data.data;
					let arr = [];
					for (let i in data) {
						arr.push(data[i])
					}
					this.sectionList = arr;
					this.sectionList = this.sectionList.concat(data[0].children);
				}
			})
		},
		handleClose1(done){
			this.typeIndex = 0;
			this.dataIndex = 0;
			done()
		},
		// 重置
		reset() {
			this.user_id = '';
			this.teacherList();
		},
		// 选择老师
		teachTap(item) {
			for (let i in this.teacher_list) {
				for (let j in this.teacher_list[i].data) {
					for (let t in this.teacher_list[i].data[j].teacher) {
						if (this.teacher_list[i].data[j].teacher[t].teacher_id == item.teacher_id && !this.teacher_list[i].data[j].teacher[t].checked) {
							this.teacher_list[i].data[j].teacher[t].checked = true;
						} else {
							this.teacher_list[i].data[j].teacher[t].checked = false;
						}
					}
				}
			}
			this.user_id = item.teacher_id;
		},
		unique(arr) {
			// 根据唯一标识orderId来对数组进行过滤
			const res = new Map(); //定义常量 res,值为一个Map对象实例 //返回arr数组过滤后的结果，结果为一个数组   过滤条件是，如果res中没有某个键，就设置这个键的值为1
			return arr.filter(arr => !res.has(arr) && res.set(arr, 1));
		},
		unique2(arr) {
			// 根据唯一标识orderId来对数组进行过滤
			const res = new Map(); //定义常量 res,值为一个Map对象实例 //返回arr数组过滤后的结果，结果为一个数组   过滤条件是，如果res中没有某个键，就设置这个键的值为1
			return arr.filter(arr => !res.has(arr.teacher_id) && res.set(arr.teacher_id, 1));
		},
		twacherTap(index) {
			this.MeunIndex = index;
		},
		// 切换分类
		typeTap(index) {
			this.typeIndex = index;
			this.dataIndex = 0;
		},
		dataTap(index) {
			this.dataIndex = index;
		},
		// 教师列表
		teacherList() {
			this.$api.pepople.teacherList({}).then(res => {
				if (res.data.code == 1) {
					this.teacher_list = res.data.data;
					for (let i in this.teacher_list) {
						for (let j in this.teacher_list[i].data) {
							for (let t in this.teacher_list[i].data[j].teacher) {
								this.nums = this.nums.concat(this.teacher_list[i].data[j].teacher[t].teacher_id);
								this.selectList = this.selectList.concat(this.teacher_list[i].data[j].teacher[t]);
							}
						}
					}
					this.nums = this.unique(this.nums);
					this.selectList = this.unique2(this.selectList);
				}
			});
		},
		tapType(id){
			this.sid = id;
			this.page = 1;
			this.getAssetList();
		},
		closeDialog(){
			this.addVisible = false;
			this.editVisible = false;
			this.storage = false;
			this.editstorage = false;
			this.reset();
		},
		handleSizeChange(val) {
			console.log(`每页 ${val} 条`);
		},
		handleCurrentChange(val) {
			this.page = val;
			this.getAssetList()
		},
		tabTap(index) {
			if(index==1){
				this.$router.push('/supplies');
			}
			this.tabIndex = index;
		},
		handleClose(done) {
			done();
			this.reset();
		},
		typeChange(e){
			this.category_id = e[0];
			this.category_ids = e[1];
		},
		// 获取房间列表
		getPlaceRoomList() {
			this.$api.setting.getPlaceRoomList({}).then(res => {
				if (res.data.code == 1) {
					this.roomList = res.data.data.room.rows;
				}
			});
		},
		// 获取分类列表
		getAssetTypeList() {
			this.$api.asset.getAssetTypeList({filter: JSON.stringify({mold:2})}).then(res => {
				if (res.data.code == 1) {
					let data = res.data.data;
					for (let i in data) {
						data[i].isShow = false;
						data[i].isbtn = false;
						for (let j in data[i].children) {
							data[i].children[j].checked = true;
							data[i].children[j].isbtn = false;
						}
					}
					this.assetTypeList = data;
				}
			});
		},
		// 添加分类
		addtype(pid) {
			this.pid = pid;
			this.name = '';
			this.addVisible = true;
		},
		submitaddtype() {
			let data = {
				name: this.name,
				pid: this.pid,
				mold: 2
			};
			this.$api.asset.addAssetType(data).then(res => {
				if (res.data.code == 1) {
					this.$message.success('添加成功');
					this.addVisible = false;
					this.closeDialog();
					this.getAssetTypeList();
				} else {
					this.$message.error(res.data.msg);
				}
			});
		},
		// 编辑分类弹窗
		editShow(item) {
			this.pid = item.pid;
			this.name = item.name;
			this.typeId = item.id;
			this.editVisible = true;
		},
		submitaddedit() {
			let data = {
				name: this.name,
				pid: this.pid,
				mold: 2,
				id:this.typeId
			};
			this.$api.asset.editAssetType(data).then(res => {
				if (res.data.code == 1) {
					this.$message.success('修改成功');
					this.editVisible = false;
					this.closeDialog();
					this.getAssetTypeList();
				} else {
					this.$message.error(res.data.msg);
				}
			});
		},
		// 删除分类
		delPlace(id) {
			let _this = this;
			_this
				.$confirm('确定要删除？', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消'
				})
				.then(() => {
					_this.$api.asset
						.delAssetType({
							id: id
						})
						.then(res => {
							if (res.data.code == 1) {
								_this.$message.success('删除成功');
								_this.getAssetTypeList();
							} else {
								_this.$message.error(res.data.msg);
							}
						});
				});
		},
		// 获取资产列表
		getAssetList() {
			let data = {
				mold:2
			}
			if(this.sid){data.category_ids = this.sid}
			if(this.year_id){data.year_id = this.year_id}
			if(this.semester_ids){data.semester_id = this.semester_ids}
			this.$api.asset.getAssetList({
				page:this.page,
				limit:this.limit,
				filter: JSON.stringify(data)
			}).then(res => {
				if (res.data.code == 1) {
					this.assetList = res.data.data.rows;
					this.total = res.data.data.total;
				}
			});
		},
		// 资产入库
		submit(){
			if(this.yearval==''||!this.yearval){return this.$message.error('请选择学年');}
			if(this.semester_id==''||!this.semester_id){return this.$message.error('请选择学期');}
			if(this.assetname==''||!this.assetname){return this.$message.error('请输入资产名称');}
			if(this.category_id==''||!this.category_id){return this.$message.error('请选择分类');}
			if(this.roomid==''||!this.roomid){return this.$message.error('请选择位置');}
			if(this.storagetime==''||!this.storagetime){return this.$message.error('请选择入库时间');}
			if(this.number==''||!this.number){return this.$message.error('请输入物品数量');}
			if(this.units==''||!this.units){return this.$message.error('请输入物品单位');}
			let data = {
				year_id:this.yearval,
				semester_id:this.semester_id,
				code:this.code,
				name:this.assetname,
				units:this.units,	
				origin:this.origin,
				storagetime:this.storagetime,
				mold:2,
				number:this.number,
				money:this.money,	
				category_id:this.category_id,
				category_ids:this.category_ids,
				school_place_room_id:this.roomid,
				user_id:this.user_id,
				store_type:this.store_type,
				purpose:this.purpose,
				sku:this.sku,
				supplier:this.supplier,
				amount:this.amount,
				school_department_id:this.school_department_id,
				proof:this.proof
			}
			this.$api.asset.assetStroage(data).then(res=>{
				if(res.data.code==1){
					this.$message.success('添加成功');
					this.storage = false;
					this.closeDialog();
					this.getAssetList();
				}else{
					this.$message.error(res.data.msg);
				}
			})
			
		},
		reset(){
			this.name = '';
			this.pid = '';
			this.assetname = '';
			this.storagetime = '';
			this.units = '';
			this.number = '';
			this.money = '';
			this.category_id = '';
			this.category_ids = '';
			this.asseTypeId = [];
			this.roomid = '';
			this.code = '';
			this.origin = '';
			this.user_id = '';
			this.store_type = 1;
			this.purpose = '';
			this.sku = '';
			this.supplier = '';
			this.amount = '';
			this.school_department_id = '';
			this.proof = '';
		},
		// 编辑资产
		openDialog(e){
			this.id = e.id;
			this.assetname = e.name;
			this.storagetime = e.storagetime_text;
			this.units = e.units;
			this.number = e.number;
			this.money = e.money;
			this.category_id = e.category_id;
			this.category_ids = e.category_ids;
			this.asseTypeId[0] = this.category_id;
			this.asseTypeId[1] = this.category_ids;
			this.roomid = e.school_place_room_id;
			this.code = e.code;
			this.origin = e.origin;
			this.user_id = e.user_id;
			this.store_type = Number(e.store_type);
			this.purpose = e.purpose;
			this.sku = e.sku;
			this.supplier = e.supplier;
			this.amount = e.amount;
			this.school_department_id = e.school_department_id;
			this.proof = e.proof;
			this.yearval = e.year_id;
			this.getXueqiList(1)
			this.semester_id = e.semester_id;
			this.editstorage = true;
		},
		editSubmit(){
			if(this.yearval==''||!this.yearval){return this.$message.error('请选择学年');}
			if(this.semester_id==''||!this.semester_id){return this.$message.error('请选择学期');}
			if(this.assetname==''||!this.assetname){return this.$message.error('请输入资产名称');}
			if(this.category_id==''||!this.category_id){return this.$message.error('请选择分类');}
			if(this.roomid==''||!this.roomid){return this.$message.error('请选择位置');}
			if(this.storagetime==''||!this.storagetime){return this.$message.error('请选择入库时间');}
			if(this.number==''||!this.number){return this.$message.error('请输入物品数量');}
			if(this.units==''||!this.units){return this.$message.error('请输入物品单位');}
			let data = {
				year_id:this.yearval,
				semester_id:this.semester_id,
				id:this.id,
				name:this.assetname,
				units:this.units,	
				origin:1,
				storagetime:this.storagetime,
				mold:2,
				number:this.number,
				money:this.money,	
				category_id:this.category_id,
				category_ids:this.category_ids,
				school_place_room_id:this.roomid,
				user_id:this.user_id,
				store_type:this.store_type,
				purpose:this.purpose,
				sku:this.sku,
				supplier:this.supplier,
				amount:this.amount,
				school_department_id:this.school_department_id,
				proof:this.proof
			}
			this.$api.asset.assetEdit(data).then(res=>{
				if(res.data.code==1){
					this.$message.success('修改成功');
					this.getAssetList();
					this.closeDialog();
				}else{
					this.$message.error(res.data.msg);
				}
			})
		},
		// 导出
		deriveAset(){
			this.$api.asset.deriveAset({}).then(res=>{
				if(res.data.code==1){
					this.$message.success('导出成功');
					// location.href = '';
				}else{
					this.$message.error(res.data.msg);
				}
			})
		}
	}
};
</script>

<style lang="scss">
#asset {
	.nav-tab-bar-box {
		background-color: #ffffff;
		padding: 0.72vw;
		margin-top: 1.04vw;
		.nav-tab-bar {
			.nav-tab-item {
				color: #6e7a87;
				padding: 0 0.78125vw;
				border-right: 1px solid #f3f3f3;
				cursor: pointer;
				-webkit-touch-callout: none;
				-webkit-user-select: none;
				-khtml-user-select: none;
				-moz-user-select: none;
				-ms-user-select: none;
				user-select: none;
			}
			&div:last-child {
				border-right: none;
			}
			.active {
				font-size: 0.833vw;
				color: #63acff;
				position: relative;
				font-weight: bold;
			}
			.active::after {
				position: absolute;
				content: '';
				height: 2px;
				left: 0.78125vw;
				right: 0.78125vw;
				background-color: #63acff;
				bottom: -0.78125vw;
			}
		}
	}
	.content {
		margin-top: 20px;
		background-color: #ffffff;
		height: 78vh;
		.left {
			border-right: 1px solid #eeeeee;
			height: 78vh;
			min-width: 300px;
			.add {
				background-color: #f8f8f8;
				padding: 10px 0;
				font-size: 24px;
				color: #8d9da6;
				cursor: pointer;
			}
			.add:hover {
				color: #007aff;
			}
			.building-list {
				padding: 25px;
				.item {
					padding: 0 15px;
					height: 40px;
					line-height: 40px;
					margin-top: 15px;
					background-color: #f8f8f8;
					.el-icon-caret-right,
					.el-icon-caret-bottom {
						color: #a4a4a4;
						margin-right: 5px;
						font-size: 16px;
					}
					.el-checkbox {
						display: block;
					}
					.btn {
						i {
							background-color: #e8eaec;
							color: #a4a4a4;
							width: 25px;
							height: 25px;
							margin-left: 10px;
							border-radius: 50%;
						}
						i:hover {
							color: red;
						}
					}
				}
				.floor-list {
					background-color: #f8f8f8;
					border-top: 1px solid #eeeeee;
					padding: 0 15px 15px 30px;
					.floor-item {
						background-color: #f1f1f1;
						padding: 0 15px 0 25px;
						color: #4c5559;
						height: 40px;
						line-height: 40px;
						margin-top: 15px;
						cursor: pointer;
						.btn {
							i {
								background-color: #ffffff;
								color: #a4a4a4;
								width: 25px;
								height: 25px;
								margin-left: 10px;
								border-radius: 50%;
							}
							i:hover {
								color: red;
							}
						}
					}
					.active {
						background-color: #e4f1ff;
						color: #007aff;
					}
				}
			}
		}
		.right {
			height: 78vh;
			width: 100%;
			padding: 25px 50px 0 50px;
			.title {
				padding: 0 0 15px 0;
			}
		}
	}

	.el-dialog__header {
		border-bottom: 1px solid #eeeeee;
		padding: 10px 20px;
		.el-dialog__title {
			font-size: 16px;
		}
	}
	.el-dialog__body {
		padding: 0 20px 35px 20px;
		overflow-y: auto;
		max-height: 600px;
		.edit-box {
			.input-box {
				margin-top: 35px;
				padding-left: 15px;
				.input-lebal {
					color: #aeb5b8;
					margin-right: 10px;
					span {
						color: #ff7a65;
						margin-right: 3px;
					}
				}
				.el-icon-remove-outline {
					font-size: 1.5625vw;
					color: #d7d9db;
					margin-left: 10px;
				}
			}
			.avatar-uploader .el-upload {
				border: 1px dashed #d9d9d9;
				border-radius: 6px;
				cursor: pointer;
				position: relative;
				overflow: hidden;
			}
			.avatar-uploader .el-upload:hover {
				border-color: #409eff;
			}
			.avatar-uploader-icon {
				font-size: 28px;
				color: #8c939d;
				width: 90px;
				height: 90px;
				line-height: 90px;
				text-align: center;
			}
			.avatar {
				width: 90px;
				height: 90px;
				display: block;
			}
		}
	}
	.el-dialog__footer {
		padding: 15px 20px 15px;
		box-shadow: 0px -5px 10px -5px #eeeeee;
	}

	.el-table__column-filter-trigger {
		.el-icon-arrow-down:before {
			content: '\E790';
			font-size: 16px;
		}
	}
}
.selectShow {
	.el-dialog__header {
		border-bottom: 1px solid #eeeeee;
		padding: 10px 20px;
		.el-dialog__title {
			font-size: 16px;
		}
	}
	.el-dialog__body {
		padding: 0;
		.user-list-box {
			.user-list-head {
				padding: 10px 20px;
				border-bottom: 1px solid #eeeeee;
				.user-type-list {
					.user-type-item {
						color: #8d9da6;
						cursor: pointer;
					}
					.active {
						color: #007aff;
						position: relative;
					}
					.active::after {
						position: absolute;
						width: 25px;
						height: 2px;
						background-color: #007aff;
						content: '';
						bottom: -5px;
						left: 0;
					}
					span {
						color: #cccccc;
						margin: 0 8px;
					}
				}
			}
			.user-list-content {
				height: 40vh;
				.left {
					height: 98%;
					overflow-y: auto;
					border-right: 1px solid #eeeeee;
					padding-top: 5px;
					.left-item {
						padding: 0 20px;
						height: 40px;
						line-height: 40px;
						color: #a4a4a4;
						cursor: pointer;
						white-space: nowrap;
						overflow: hidden;
						text-overflow: ellipsis;
					}
					.active {
						background-color: #e4f1ff;
						color: #2d8cf0;
						position: relative;
					}
					.active:after {
						position: absolute;
						top: 0;
						bottom: 0;
						right: 0;
						width: 2px;
						content: '';
						background-color: #2d8cf0;
					}
				}
				.right {
					height: 37vh;
					padding: 10px 25px;
					overflow-y: auto;
					.list {
						display: flex;
						flex-wrap: wrap;
						.item {
							cursor: pointer;
							padding: 5px 15px;
							border-radius: 3px;
							background-color: #f3f3f3;
							margin-right: 10px;
							margin-bottom: 10px;
						}
						.active {
							background-color: #e4f1ff;
							color: #2d8cf0;
						}
					}
				}
			}
		}
	}
	.dialog-title {
		padding: 10px 0 5px 0;
	}
	.el-dialog__footer {
		padding: 15px 20px 15px;
		box-shadow: 0px -5px 10px -5px #eeeeee;
	}
}
</style>
